Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1d  Compiler error at line 15.  Both class A and B are declared in the same package. Therefore, class B has access to the public, protected, and package access methods of class A.  
2e  Compiler Error  A static method can not access a non-static variable.  
3b  c  d  Compiler error at line 2.  Compiler error at line 3.  Compiler error at line 4.  Only one class in a source code file can be declared public. The other classes may not be public. Therefore, the declarations for classes Basics2, Basics3, and Basics4 generate errors.  
4a  b  e  f  g  h  final  private  static  synchronized  native  strictfp  A final or private method can not be overridden and therefore can not be abstract. An abstract method declaration provides no implementation of the method an all implementation details are left to the overriding method in the subclass. Since the synchronized modifier specifies an implementation detail that can be ignored by an overriding subclass method, it makes no sense to allow the use of the sychronized modifier in an abstract method declaration. The strictfp modifier in a superclass declaration has no impact on an overriding method in a subclass. For that reason, it makes no sense to include the strictfp modifier in an abstract method declaration.  
5e  Prints: 2,5,8  Arrays a1, a2 and a3 all contain 3 integers. The first element of the array has an index of 0 so and index of one refers to the second element of each array.  
6d  e  Compiler error at line 18.  Compiler error at line 19.  Class A and C are not declared in the same package; therefore, class C does not have access to package access method m4. Since class C extends class A, class C does have access to the protected method, m2, of class A.  
7e  Prints: 2,5,9  The line marked 1 declares three arrays, a2, a3 and a4 where the elements of the arrays are primitives of type int. The line marked 2 declares an array, a1, where each element of a1 is an array of primitives. The elements of a1 are initialized with references to the previously declared arrays.  
8i  volatile  A field might be shared between two or more threads. Each thread is allowed to maintain a working copy of the field. If the threads do not reconcile the working copies then each might be working with a different value. The volatile modifier is used to force each thread to reconcile its working copy of the field with the master copy in main memory. For more information, please see section 8.3.1.4 of the Java Language Specification.  
9e  f  Compiler Error at 3.  Compiler Error at 4.  There are no compiler errors at 1 and 2 because the variables are compile time constants that are assignable to type byte. There are compiler errors at 3 and 4 because the variables are not compile time constants and are not assignable to type byte without an explicit cast. For more information, please see section 5.2 of the Java Language Specification for more information on assignment conversions.  
10d  e  f  Compiler Error at 2.  Compiler Error at 3.  Compiler Error at 4.  There is a compiler error at 2 because the method is declared with a void return type so the return statement is not permitted to return a value. There is a compiler error at 3 because the method is declared with an int return type but the return statement does not return a value. There is a compiler error at 4 because method m2 is declared with a void return type. Please see section 14.16 of the Java Language Specification for more information on the return statement.  
11e  None of the above.  An abstract method can override an abstract method. The overridding abstract method can be useful as a place for comments. An abstract method can override a method that is not abstract. An abstract method declaration does provide a throws clause that can be empty. The body of an abstract method is represented by a semicolon.  
12a  d  class A extends Object.  Compiler error at 3.  The constructor for class B and class C both invoke the constructor for A. The constructor for class A declares Exception in the throws clause. Since the constructors for B and C invoke the constructor for A implicitly, both B anc C must declare Exception in their throws clause. A compile time error is generated at marker 3 because the constructor does not declare Exception in the throws clause.  
13c  d  e  private  protected  public  Constructors are not inherited and can not be overridden. For that reason, there is no need to apply the "final" modifier to a constructor declaration. For the same reason, an abstract constructor would be useless since it could never be implemented. A constructor is used to create an instance of an object so it would make no sense to apply the "static" modifier in an attempt to disassociate the constructor from an object instance. An object is not available to multiple threads during the construction process so the "synchronized" modifier would not provide additional protection. Native constructors are not permitted because the JVM could not easily monitor the operation of a native constructor. A constructor is made strictfp by declaring the class strictfp.  
14h  i  Compiler Error at 4.  Compiler Error at 5.  There are no compiler errors at 1, 2 or 3 because a widening conversion from type byte, char, or short to type int is legal. There are compiler errors at 4 and 5 because a narrowing conversion is not a legal assignment conversion without an explicit cast. For more information, please see section 5.2 of the Java Language Specification for more information on assignment conversions. 
15a  b  c  e  A static method is also known as a class method.  A class method is not associated with a particular instance of the class.  The keyword "this" can not be used inside the body of a static method.  A method that is not static is known as an instance method.  The keyword "this" refers to the current instance of the class. Since a static method is not associated with an instance, the keyword "this" is not permitted in the body of a static method.  
16b  d  The compiler attempts to create a default constructor for class B.  Compiler error at 2.  If no constructor is declared explicitly, then the compiler will implicitly create a default constructor that accepts no parameters, has no throws clause, and invokes its superclass constructor. Since class A has an explicitly declared constructor, the compiler will not create an implicit default constructor. Class B does not have an explicit constructor declaration so the compiler attempts to create a default constructor. Since class A does not have a no parameter constructor, the attempt by class B to invoke the no parameter constructor of A would fail. As a result, a compiler error is generated at marker 2.  
17a  b  c  d  e  f  1  2  3  4  5  6  A variable that is local to a method can not possibly be access from outside of the class so the access modifiers are not useful and not legal. A variable that is local to a method can not possibly be part of the persistent state of an object so the transient modifier is not useful and not legal. Local variables can not possible be shared between threads so the volatile modifier is not useful and not legal. A local variable can be declared final to prevent its value from being assigned more than once. If the value of the variable needs to be accessed from a local class or an anonymous class, then the local variable or method parameter must be declared final.  
18a  c  e  f  g  The value of a final field can not be assigned more than once.  A final field that is not assigned a value at compile time is called a blank final variable.  A blank final variable that is static must be definitely assigned in a static initializer.  At the end of the instance construction process all blank final variables must be definitely assigned.  A field can not be declared both final and volatile.  Static and non--static field variables may be declared final. All final fields must be definitely assigned a value once and only once. If the final variable is not assigned a value at compile time then it is called a blank final variable. All blank final static variables must be assigned in a static initializer. All blank non-static variables must be assigned by the end of the instance construction process. A field is sometimes shared between threads. The volatile modifier is used to force threads to reconcile their own working copy of a field with the master copy in main memory. If a field is declared final then its value does not change and there is no need for threads to reconcile their own working copies of the variable with the master copy in main memory.  
19a  Prints: 2,3,4,0,  The array, a, is an array containing four arrays. The size of the first is 2, the second is 3, the third is four, and the fourth is zero.  

Copyright © 2002, Dan Chisholm
All rights reserved.
1